﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_trainbuy officer_trainbuy()
        {
            string userCode = SangoManager.Authentication();

            officer_trainbuy response = ReflectionManager.CreatePojo<officer_trainbuy>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);

            if (property.Trainingcount >= SysVipSettingHelper.search(profile.Viplevel).Officerupgradeqty)
            {
                response.Message = "当前VIP等级不足，无法扩展训练位";
                return response;
            }


            Entities.Sys_Queue queue = UsrQueueDao.getAvailableSysQueue(userCode, CtiQueueTypecodeCollection.OFFICER_UPGRADE);

            if (queue == null)
            {
                response.Message = "无训练为可购买";
                return response;
            }

            long goldprice = queue.Goldprice;

            if (!UsrProfileDao.tryDecreaseGoldValue(profile, goldprice))
            {
                response.Message = "金币不足";
                return response;
            }

            Entities.Usr_Queue usrQueue = UsrQueueDao.createQueue(profile.Usercode, profile.Nickname, queue);
            UsrPropertyDao.trainBuy(property);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Insert(usrQueue);

                command.Update(property);

                command.Update(profile);

                command.Commit();
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.property = UsrProfileFacade.ToProperty(property);
            response.queue = UsrQueueFacade.ToQueue(usrQueue);
            response.Succeed = true;
            return response;
        }


    }
    public interface officer_trainbuy : ISangoResponse
    {
        player_profile profile { get;set;}

        player_property property { get;set;}

        player_queue queue { get;set;}
    }
}
